<template>
  <!-- /**
     *
     已录入 ---1
     待交件 ---2
     待放款 ---3
     已放款 ---4
     已回款 ---5
     已中止 ---6
     挂账   ---7
       重置录入 ---8
     重置审批 ---9
     待跟进 --- 11
     已结清 ---12
     */功能区
  -->
  <div>
    <div class="first">
      <h1 class="title_sub gn">功能区：</h1>
      <span v-if="showUpdateStatusBtn && orderStatus == 2">
        <el-button type="primary" size="small" @click="putUpdateStatus(3)"
          >标记已交件</el-button
        >
      </span>
      <span v-if="showUpdateStatusBtn && orderStatus == 2">
        <el-button type="primary" size="small" @click="putUpdateStatus(-2)"
          >标记交件失败</el-button
        >
      </span>
      <span v-if="showChargeNoBtn && orderStatus == 3">
        <el-button type="primary" size="small" @click="putUpdateStatus(4)"
          >标记已放款</el-button
        >
      </span>
      <span v-if="showChargeNoBtn && orderStatus == 3">
        <el-button type="primary" size="small" @click="putUpdateStatus(-3)"
          >标记为放款失败</el-button
        >
      </span>
      <span v-if="showChargeNoBtn && orderStatus == 5">
        <el-button type="primary" size="small" @click="putUpdateStatus(5)"
          >重置为已放款进行收款</el-button
        >
      </span>

      <span v-if="showReturnedStatusList && (orderStatus == 4 || orderStatus == 12)">
        <el-button type="primary" size="small" @click="putUpdateStatus(5)"
          >标记为已归档</el-button
        >
      </span>
      <span v-if="showReturnedStatusList && orderStatus == 4">
        <el-button type="primary" size="small" @click="settleDialog=true"
        >标记为已结清</el-button
        >
      </span>
      <el-button
        type="danger"
        size="small"
        v-if="markUpdate > 0 && showMarkUpdate"
        @click="markUpdateFun"
        >处理此订单更新</el-button
      >
      <!-- <el-button
        type="danger"
        size="small"
        v-if="markUpdate == 0 && showMarkUpdate"
        >我已处理此订单更新</el-button
      > -->
    </div>
    <!-- 提交不动产弹框 -->
    <el-dialog
      title="填写信息"
      :visible.sync="dialogVisible"
      width="1000px"
      :before-close="handleClose"
      v-if="dialogVisible"
    >
      <BdcDialog
        :orderNumber="orderNumber"
        :isBdc="isBdc"
        ref="BdcDialog"
        @dialogCloes="dialogCloes"
      ></BdcDialog>
    </el-dialog>
    <!--    结清弹窗-->
    <el-dialog title="结清时间" @close="cancelSettle" :visible.sync="settleDialog">
      <el-form >
        <el-form-item label="结清时间:" >
          <el-date-picker
            v-model="settlementTime"
            type="datetime"
            placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSettle">取 消</el-button>
        <el-button type="primary" @click="confirmSettle('12')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  putUpdateStatus,
  markUpdate,
  getProductUrl
} from "../../assets/js/api";
import { checkPermiss } from "../../assets/js/utlis";
import BdcDialog from "./BdcDialog";
export default {
  name: "FunctionalAreas",
  props: ["isBdc"],
  components: { BdcDialog },
  created() {
    this.golbalPermissions = localStorage.getItem("permissions").split(",");
    this.orderNumber = sessionStorage.getItem("orderNumber");
    this.findProductUrl(this.orderNumber);
  },
  data() {
    return {
      orderStatus: "",
      orderNumber: "",
      markUpdate: "",
      markForm: {
        orderNumber: "",
        markUpdate: 0
      },
      golbalPermissions: [],
      ProductUrl: "",
      dialogVisible: false,
      ruleForm: {
        jkr_name: "",
        jkr_id_car: "",
        jkr_phone: "",
        jkr_attr: "",
        jkr_comp: "",
        qd_name: "",
        qd_phone: "",
        po_name: "",
        po_id_car: "",
        po_phone: "",
        po_comp: "",
        po_attr: "",
        loan_bank: "",
        dyr_name: "",
        dyr_id_car: "",
        dyr_phone: "",
        house_num: "",
        house_attr: "",
        house_size: "",
        house_area: "",
        house_lp: ""
      },
      rules: {
        jkr_name: [
          { required: true, message: "请输入借款人姓名", trigger: "blur" }
        ],
        jkr_id_car: [
          { required: true, message: "请输入借款人身份证", trigger: "blur" },
          {
            pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: "请填写正确的身份证号码",
            trigger: "blur"
          }
        ],
        jkr_phone: [
          { required: true, message: "请输入借款人电话", trigger: "blur" },
          {
            pattern: /^1[345789]\d{9}$/,
            message: "请填写正确的手机号码",
            trigger: "blur"
          }
        ],
        jkr_attr: [
          { required: true, message: "请输入借款人联系地址", trigger: "blur" }
        ],
        dyr_name: [
          { required: true, message: "请输入抵押人姓名", trigger: "blur" }
        ],
        dyr_id_car: [
          { required: true, message: "请输入抵押人身份证", trigger: "blur" },
          {
            pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: "请填写正确的身份证号码",
            trigger: "blur"
          }
        ],
        dyr_phone: [
          { required: true, message: "请输入抵押人电话", trigger: "blur" },
          {
            pattern: /^1[345789]\d{9}$/,
            message: "请填写正确的手机号码",
            trigger: "blur"
          }
        ],
        house_num: [
          { required: true, message: "请输入产权证号", trigger: "blur" }
        ],
        house_attr: [
          { required: true, message: "请输入房屋地址", trigger: "blur" }
        ],
        house_size: [
          { required: true, message: "请输入房屋面积", trigger: "blur" }
        ],
        house_area: [
          { required: true, message: "请输入所在区域", trigger: "blur" }
        ],
        house_lp: [
          { required: true, message: "请输入楼盘名称", trigger: "blur" }
        ]
      },
      settleDialog: false,//结清弹窗控制
      settlementTime:'',//结清时间
    };
  },
  computed: {
    //订单更新按钮
    showMarkUpdate: function() {
      return checkPermiss("business:order:markUpdate", this.golbalPermissions);
    },
    // 待交件-标记(已交件,交件失败)按钮
    showUpdateStatusBtn: function() {
      return checkPermiss(
        "business:order:updateStatus",
        this.golbalPermissions
      );
    },
    //标记已归档按钮
    showReturnedStatusList: function() {
      return checkPermiss(
        "business:order:returnedStatusList",
        this.golbalPermissions
      );
    },
    //财务信息添加按钮(标记已放款，标记放款失败TODO)
    showChargeBtn: function() {
      return checkPermiss("business:upForm:charge", this.golbalPermissions);
    },
    //标记放款，放款失败
    showChargeNoBtn: function() {
      return checkPermiss("business:upForm:chargeNo", this.golbalPermissions);
    }
  },
  methods: {
    //确认结清
    confirmSettle(statusId){
      this.putUpdateStatus(statusId,this.settlementTime);
    },
    //取消结清弹窗
    cancelSettle(){
      this.settleDialog=false;
      this.settlementTime='';
    },
    /**
     * 标记已交件 statusId ==3
     * 交件失败 statusId ==-2
     * 标记已放款 statusId ==4
     * 放款失败 statusId ==-3
     * 标记为已归档 statusId ==5
     * 标记为已归档 statusId ==7
     */
    putUpdateStatus(statusId,clearedTime='') {
      //标记已交件
      // if (this.isBdc == 0) {
      putUpdateStatus({
        orderNumber: this.orderNumber,
        statusId: statusId,
        clearedTime:clearedTime
      }).then(res => {
        if (res.code == 200) {
          this.$message({
            message: res.message,
            type: "success"
          });
          this.$router.replace({ path: this.ProductUrl });
        } else {
          this.$message({
            message: res.message,
            type: "error"
          });
        }
      });
      // } else if (this.isBdc == 1) {
      //   this.dialogVisible = true;
      // }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
          this.$refs.BdcDialog.closeFun(true);
        })
        .catch(() => {});
    },
    dialogCloes() {
      this.dialogVisible = false;
    },
    //根据订单查询路由path路径
    findProductUrl(orderNumber) {
      getProductUrl({
        orderNumber: orderNumber
      }).then(res => {
        this.ProductUrl = res.data.productUrl;
        this.markUpdate = res.data.markUpdate;
        this.orderStatus = res.data.orderStatus;
      });
    },
    //处理订单更新
    markUpdateFun() {
      this.markForm.orderNumber = this.orderNumber;
      markUpdate(this.markForm).then(res => {
        if (res.code == 200) {
          this.$message({
            message: res.message,
            type: "success"
          });
          this.$router.replace({ path: this.ProductUrl });
        } else {
          this.$message({
            message: res.message,
            type: "error"
          });
        }
      });
    }
  }
};
</script>

<style scoped type="less">
.first {
  line-height: 35px;
  text-align: left;
  overflow: hidden;
}
h1 {
  float: left;
}
span {
  margin-right: 10px;
}
::v-deep .el-dialog__footer {
  text-align: center;
}
/deep/ .el-form-item__content{
  text-align: left;
}
/deep/ .el-form-item{
  display: flex;
  justify-content: center;
}
</style>
